//
//  pickUpViewController.swift
//  NewLndd
//
//  Created by 邵禹曦 on 2020/1/13.
//  Copyright © 2020 辽宁当代. All rights reserved.
//

import UIKit
// MARK: - 摘单
class pickUpViewController: HomeApiViewController {
    
    // MARK: - 绑定标签
     // 挂单号
     @IBOutlet weak var ON: UILabel!
     // 商品代码
     @IBOutlet weak var COI: UILabel!
     // 商品名称
     @IBOutlet weak var CON: UILabel!
     // 挂单价格
     @IBOutlet weak var OP: UILabel!
     // 挂单总量
     @IBOutlet weak var OQ: UILabel!
     // 剩余挂单量
     @IBOutlet weak var SQ: UILabel!
     // 挂单方代码
     @IBOutlet weak var BI: UILabel!
     // 挂单时间
     @IBOutlet weak var OT: UILabel!
     // 摘单数量
     @IBOutlet weak var zdsl: UITextField!
     // 提交
     @IBOutlet weak var tj: UIButton!
     // 返回
     @IBOutlet weak var ret: UIButton!
var pickUp : pickUp?
    override func viewDidLoad() {
        super.viewDidLoad()
showInfo()
        // Do any additional setup after loading the view.
    }
    


}


extension pickUpViewController {
    private func showInfo() {
            // GCD
            let group = DispatchGroup()
     
            // 从沙盒中获取参数
             var userPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
             userPath =  (userPath as NSString).appendingPathComponent("data.plist")
             user = NSKeyedUnarchiver.unarchiveObject(withFile: userPath) as? User
        
        Thread.sleep(forTimeInterval: 0.05)


            // 用户验证
            self.u = user?.U!
            self.retcode = user?.RETCODE!
            // 用户验证
            let str = [u,"101",retcode]
             
            //self.isLogin = self._network(str: str as! [String], group: group)
        group.enter()
        NetworkTools.share.check_user(code: str as Array<Any>) { (result, error) in
                      
                          if error != nil {
                              print("no no no")
                              return
                          }
                          

                          if Int(self._retcode(res: result!))! < 0 {
                              self.isLogin = false
                          } else {
                              self.isLogin = true
                          }
                          group.leave()
                      }

            group.notify(queue: DispatchQueue.main) {
                if !self.isLogin {
                    return self.setupVisitorView()
                } else {
                    Thread.sleep(forTimeInterval: 0.05)
                    // 2.设置导航栏内容
                    //self.setupNavigationBar()
                    self.tableShouInfo()
                }
             }
        }
}

 

extension pickUpViewController {
    // 初始化
    @objc private func tableShouInfo() {
        
        self.ON.text! = (self.pickUp?.ON!)!
        self.COI.text! = (self.pickUp?.COI!)!
        self.CON.text! = (self.pickUp?.CON!)!
        self.OP.text! = (self.pickUp?.OP!)!
        self.OQ.text! = (self.pickUp?.OQ!)!
        self.SQ.text! = (self.pickUp?.SQ!)!
        self.BI.text! = (self.pickUp?.BI!)!
        self.OT.text! = (self.pickUp?.OT!)!
        
        setupView()
    }
    
    
    @objc func setupView() {
        // 监听登陆按钮
        tj.addTarget(self, action: #selector(loginBtnClick), for: .touchUpInside)

        // 返回上一页按钮
        //ret.addTarget(self, action: #selector(backBtnClick), for: .touchUpInside)
    }
    
    // 绑定登陆按钮
    @objc private func loginBtnClick()
    {
        guard self.zdsl.text != "" else {
            if #available(iOS 13.0, *) {
                self.showMsgbox(_message: "摘单数量不能为空")
            }
            return
        }
        let sta : String = (self.pickUp?.sta)!
        let coi : String = (self.pickUp?.COI)!
        let on : String = (self.pickUp?.ON)!
        let op : String = (self.pickUp?.OP)!
        let zdsl : String = self.zdsl.text!
        
        let str = [user?.U!,user?.RETCODE!,"" ,coi, on, op, sta,"2", zdsl]
        
        NetworkTools.share.loadorder(code: str as Array<Any>) { (result, error) in
            
            if error != nil {
                print("no no no")
                return
            }
            

            
            // 获取可选类型中的数组
            guard let resultArray = result else {
                print("no")
                return
            }
            
            
            print(resultArray)
            if let REP = resultArray["REP"] as? [String : AnyObject] {
                if let arr = REP["RESULT"] as? [String : AnyObject]   {
                    let code : Int = Int(arr["RETCODE"] as! String)!
                    if(Int(truncating: NSNumber(value: code)) > 0) {
                        if #available(iOS 13.0, *) {
                            self.showMsgbox(_message: "请求成功")
                        }
                    } else {
                        if #available(iOS 13.0, *) {
                            self.showMsgbox(_message: "请求失败")
                        }
                    }
                }
            }
            
            /*
             成功返回
             ["REP": {
                 "@attributes" =     {
                     name = order;
                 };
                 RESULT =     {
                     ON = 191224000001005;
                     OT = "2019-12-24 16:51:48";
                     RETCODE = 307300021000;
                     TR = "\U5355\U53f7\U4e3a191224000001005\U7684\U59d4\U6258\U4ece\U4e0b\U5355\U5230\U8fd4\U56de\U5ba2\U6237\U7aef\U8017\U65f640\U6beb\U79d2";
                 };
             }]
             */
            
            /*
            if let REP = resultArray["REP"] as? [String : AnyObject] {
                if let resultList = REP["RESULT"] as? [String : AnyObject] {
                    print(resultList)
                    if(resultList.values == 0) {
                        
                    }
                }
            }*/
        }
    }
    
    // 返回上一页
    @objc private func backBtnClick()
    {
        self.dismiss(animated: true, completion: nil)
    }
}
